/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package character_11_;

import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;

/**
 *
 * @author Administrator
 */
public class Fibonacci extends RecursiveTask<Long>{
    final  long n;

    public Fibonacci(long n) {
        this.n = n;
    }
    
    @Override
    public Long compute(){
        if (n <= 20) {
            return solveFibonacciSequentially(n);
        }
        ForkJoinTask<Long> subTask = new Fibonacci(n -1).fork();
        return new Fibonacci(n -2).compute() + subTask.join();
    }
    
    static long solveFibonacciSequentially(long n){
        if (n <= 1) {
            return n;
        }
        return solveFibonacciSequentially(n -1) + solveFibonacciSequentially(n - 2);
    }
    
    
}
